<template>
<van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
  <comment-item @click-reply="$emit('click-reply',$event)" :comment='item' v-for="item in list" :key="item.com_id + ''" />
</van-list>
</template>

<script>
import { getCommentList } from '@/api/article.js'
import CommentItem from './CommentItem.vue'
export default {
  components: { CommentItem },
  props: {
    type: {
      type: [Object, String, Number],
      default: 'a'
    },
    source: {
      type: [Object, String, Number],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },

  created () {

  },

  methods: {
    async onLoad () {
      try {
        const { results, last_id: lastId, total_count: total } = await getCommentList({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        this.list.push(...results)
        this.$emit('on-succcess', total)
        this.loading = false
        if (!results.length) {
          this.finished = true
        } else {
          this.offset = lastId
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
